<template>
  <el-dialog :visible="isShow" title="分配角色" @close="closeEvent">
    <el-checkbox-group v-model="roleArr">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="closeEvent">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysRole } from '@/api/setting.js'
import { sysUser } from '@/api/user.js'
import { sysUserAssignRoles } from '@/api/employees.js'
export default {
  data() {
    return {
      isShow: false,
      roleArr: [],
      list: [],
      id: ''
    }
  },
  computed: {},
  async created() {
    // 发送请求拿到表格内的数据
    const res = await sysRole({ page: 1, pagesize: 100 })
    // 将数据赋值给list数组
    // 再将数据里面的遍历name值渲染
    this.list = res.data.rows
    console.log('获取表格数据', res.data.rows)
  },
  methods: {
    // 关闭对话框
    closeEvent() {
      this.isShow = false
      // 将勾选的数据赋值为空
      this.roleArr = []
    },
    // 拿到表格数据
    async getUserInfo(id) {
      this.id = id
      // 将点击的id发送请求
      const res = await sysUser(id)
      // 将点击行的勾选id赋值给空数组
      // 再将勾选id渲染
      this.roleArr = res.data.roleIds
      console.log('详情信息', res.data.roleIds)
    },
    // 多选提交按钮
    async submit() {
      await sysUserAssignRoles(this.id, this.roleArr)
      this.closeEvent()
    }
  }
}
</script>
<style lang="less" scoped></style>
